//index.js
//获取应用实例
const app = getApp();

Page({
  data: {
    imgurl: app.urlimg,
    //好评选择
    discuss: [
      { imgs: '/pages/icon/xiao2.png', comment: '好评', imgs1: '/pages/icon/xiao1.png', },
      { imgs: '/pages/icon/xiao4.png', comment: '中评', imgs1: '/pages/icon/xiao3.png', },
      { imgs: '/pages/icon/xiao6.png', comment: '差评', imgs1: '/pages/icon/xiao5.png', },
    ],
    discuss_num: 0,
    // 评论快捷
    select: [],
    // 店铺评分
    scoring: [
      { text: '描述相符', sele: 0, },
      { text: '物流服务', sele: 0, },
      { text: '服务态度', sele: 0, },
    ],
    order_id: 0,
    gid: 0//商品id
  },
  // 好评选择
  click: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset);

    var data = that.data.data;
    var data_index = e.currentTarget.dataset.data_index;
    var discuss = that.data.discuss;
    var discusss_index = e.currentTarget.dataset.discusss_index;

    data[data_index].discuss = discuss[discusss_index].comment;

    that.setData({
      data: data,
    })
  },

  onLoad: function (o) {
    var that = this;
    // wx.request({
    // 	url: app.url + 'api_goods_comment/label_list',
    // 	data: {
    // 	},
    // 	header: {
    // 		'content-type': 'application/json',
    // 		'token': wx.getStorageSync('token_key')
    // 	}, success: function (json) {
    // 		if (json.data.done == true) {
    //       console.log(json.data.result);
    // 			var select = comment_label(0, json.data.result)
    // 			that.setData({ select: select, data: json.data.result, order_id: o.order_id, gid: o.gid })
    // 			console.log(o.order_id)
    // 			console.log(json.data.result);
    // 		} else {
    // 			wx.showModal({
    // 				title: '提示',
    // 				content: json.data.message,
    // 			})
    // 		}
    // 	}, complete: function (json) {
    // 	}
    // })


    console.log(o.order_id);
    wx.request({
      url: app.url + 'api_goods_comment/commentFormData',
      data: {
        order_id: o.order_id,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (json) {
        if (json.data.done == true) {
          console.log(json.data.result);
          var select = comment_label(0, json.data.result.label_list);
          var goods_list = json.data.result.goods_list;
          for (var i = 0; i < goods_list.length; i++) {
            goods_list[i].goods_scoring = [];
          }
          console.log(goods_list);
          that.setData({
            select: select,
            data: goods_list,
            order_id: o.order_id,
            gid: o.gid,
          });
        } else {
          wx.showModal({
            title: '提示',
            content: json.data.message,
          })
        }
      }, complete: function (json) {

      }
    })


  },

  // 评论快捷
  text_select: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var select = that.data.select;
    if (select[index].sele == 0) {
      select[index].sele = 1
    } else {
      select[index].sele = 0
    }
    that.setData({
      select: select,
    })
  },
  //商品所对应的 描述和物流 满意情况选择
  stars_select: function (e) {
    var that = this;
    var data = that.data.data;
    var data_index = e.currentTarget.dataset.data_index;//商品排列
    var scoring_index = e.currentTarget.dataset.scoring_index;//商品对应的服务
    var stars_index = e.currentTarget.dataset.stars_index;//星星等级


    if (scoring_index == 0) {
      data[data_index].goods_description = stars_index;//描述相符
    } else if (scoring_index == 1) {
      data[data_index].goods_logistics = stars_index;//物流服务
    } else if (scoring_index == 2) {
      data[data_index].shop_select = stars_index;//物流服务
    }

    console.log(data);

    that.setData({
      data: data,
    })
    // console.log(that.data.data);
  },
  // 店铺评分
  // shop_select: function (e) {
  //   var that = this;
  //   var data = that.data.data;
  //   var scoring = that.data.scoring;
  //   //显示
  //   scoring[2].sele = e.currentTarget.dataset.index + 1;

  //   //储存评分状况
  //   for (var i = 0; i < data.length; i++) {
  //     data[i].shop_select = e.currentTarget.dataset.index;
  //   }

  //   // console.log(data);
  //   that.setData({
  //     data: data,
  //     scoring: scoring,
  //   })
  // },
  //获取用户评论文字
  charchange: function (e) {
    var that = this;
    var data = that.data.data;
    data[e.currentTarget.dataset.data_index].contents = e.detail.value;
  },
  //添加图片
  add_img: function (e) {
    var _this = this;
    var data_index = e.currentTarget.dataset.data_index;
    var data = _this.data.data;
    if (data[data_index].add_img == undefined && data[data_index].add_img_upload == undefined) {
      data[data_index].add_img = [];
      data[data_index].add_img_upload = [];
    }
    if (data[data_index].add_img.length < 9) {
      wx.chooseImage({
        count: 9, // 选择照片的数量（默认9张） 
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
          var tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths);
          wx.uploadFile({
            url: app.url + 'api_goods_comment/upload_img',
            header: {
              'content-type': 'application/json',
              'token': wx.getStorageSync('token_key')
            },
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
            },
            success: function (res) {
              var res_data = JSON.parse(res.data);
              if (res_data.done == true) {
                data[data_index].add_img.push(tempFilePaths[0]);
                data[data_index].add_img_upload.push(res_data.result.img);
                console.log(data);
                _this.setData({
                  data: data,
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: '系统繁忙，请稍后再试',
                  success: function (res) {
                    if (res.confirm) {

                    }
                  }
                })
              }
            }
          })
        }
      })
    }
  },




  //删除添加的图片
  delete_add_img: function (e) {
    var that = this;
    //获得点击的下标
    var data_index = e.currentTarget.dataset.data_index;
    var img_index = e.currentTarget.dataset.img_index;
    //获取图片数组
    var data = that.data.data;
    //获取用户点击删除的图片
    var image = data[data_index].add_img;

    image.splice(img_index, 1);

    that.setData({
      data: data,
    })
  },
  //提交按钮
  bindFormSubmit: function (e) {
    var that = this;
    var data = that.data.data;
    console.log(data);
    var label_id = [];//标签id
    var select = that.data.select;
    console.log(select);
    for (var i = 0; i < select.length; i++) {
      if (select[i].sele == 1) {
        label_id.push(select[i].id);
      }
    }
    // label_id = label_id.substr(0, label_id.length - 1);
    label_id = label_id.join(',');
    console.log('评论快捷：' + label_id);

    var order_id = [];// order_id  订单id
    var goods_id = [];// goods_id  商品id
    var contents = [];//评论内容
    var comment_img = [];//评论图片
    var iscuss_num = [];  //笑脸评论选择(评价等级)
    var desc_point = [];//描述等级
    var logistics_point = [];//物流等级
    var serve_point = [];//店铺服务

    var carried = true;
    for (var i = 0; i < data.length; i++) {
      order_id.push(that.data.order_id);
      goods_id.push(that.data.data[i].goods_id);

      if (data[i].add_img_upload != undefined) {
        console.log(data[i].add_img_upload);
        comment_img.push(data[i].add_img_upload);
      }
      if (data[i].contents!=undefined){
        contents.push(data[i].contents);
      }

      if (carried) {
        push_box(iscuss_num, data[i].discuss, '请选择评价等级');
      }
      if (carried) {
        push_box(desc_point, data[i].goods_description, '请对描述进行评分');
      }
      if (carried) {
        push_box(logistics_point, data[i].goods_logistics, '请对物流进行评分');
      }
      if (carried) {
        push_box(serve_point, data[i].shop_select, '请对服务态度进行评分');
      }

    }
    function push_box(data1, data2, prompt_title) {
      if (data2 == undefined) {
        //提示信息
        wx.showToast({
          title: prompt_title,
          icon: 'loading',
          duration: 1200,
        })
        console.log('提示----------' + prompt_title);
        carried = false;
        return
      } else {
        data1.push(data2);
      }
    }



    console.log('-----------------------------------------------------------');
    console.log('订单id：' + order_id);
    console.log('评论内容：' + contents);
    console.log('评论图片：' + comment_img);
    console.log(comment_img);
    console.log('笑脸评论选择(评价等级)：' + iscuss_num);
    console.log('描述等级：' + desc_point);
    console.log('物流等级：' + logistics_point);
    console.log('店铺服务：' + serve_point);



    if (carried) {

      wx.request({
        url: app.url + 'api_goods_comment/add_goods_comment',
        data: {
          order_id: order_id,
          goods_id: goods_id,
          label_id: label_id,
          contents: contents,
          comment_img: comment_img,
          discuss_num: iscuss_num,//笑脸
          desc_point: desc_point,//描述
          logistics_point: logistics_point,//物流
          serve_point: serve_point,//服务
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (json) {
          if (json.data.done == true) {
            console.log(json.data);
            //提示信息
            wx.showToast({
              title: '评论成功',
              icon: 'success',
              duration: 1000,
              success: function () {
                setTimeout(function () {
                  wx.redirectTo({
                    url: '/pages/account/order/index?fid=0',
                  })
                }, 1000)
              }, //接口调用成功的回调函数 
            })

          } else {
            wx.showModal({
              title: '提示',
              content: json.data.message,
            })
          }
        }, complete: function (json) {

        }
      })


    }

  },
});


function comment_label(cate, label_list) {


  var data = [];
  for (var i = label_list.length - 1; i >= 0; i--) {
    if (label_list[i].comment_cate == cate) {
      data.push(label_list[i]);
    }
  }

  console.log(data);
  return data;
}
